//
//  AutoAlertView.m
//  汽车大灯色温蓝牙
//
//  Created by dfwx on 2020/9/9.
//  Copyright © 2020 zhongdaodianzi. All rights reserved.
//

#import "AutoAlertView.h"

@implementation AutoAlertView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        
        UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
        
        effectview.frame = CGRectMake(0, 0, self.frame.size.width, CGRectGetHeight(self.frame));
        effectview.alpha = .8;
        [self addSubview:effectview];
        
//        self.backImageView = [[UIImageView alloc]initWithFrame:self.bounds];
//        self.backImageView.image = [UIImage imageNamed:@"vague_bg"];
//        self.backImageView.contentMode = UIViewContentModeScaleAspectFill;
//        [self addSubview:self.backImageView];
//        dispatch_async(dispatch_get_global_queue(0, 0), ^{
//            CIContext *context = [CIContext contextWithOptions:nil];
//            CIImage *ciImage = [CIImage imageWithCGImage:[NSObject screenImageWithSize:CGSizeMake(0.8*ScreenWidth, 0.8*ScreenHeight)].CGImage];
//            CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
//            [filter setValue:ciImage forKey:kCIInputImageKey];
//            //设置模糊程度
//            [filter setValue:@30.0f forKey: @"inputRadius"];
//            CIImage *result = [filter valueForKey:kCIOutputImageKey];
//            CGRect frame = [ciImage extent];
//            NSLog(@"%f,%f,%f,%f",frame.origin.x,frame.origin.y,frame.size.width,frame.size.height);
//            CGImageRef outImage = [context createCGImage: result fromRect:ciImage.extent];
//            UIImage * blurImage = [UIImage imageWithCGImage:outImage];
//            dispatch_async(dispatch_get_main_queue(), ^{
//                self.backImageView.image = blurImage;
//            });
//        });
    }
    return self;
}

- (void)setTitleString:(NSString *)titleString {
    
    _titleString = titleString;
    CGFloat titleHeight = [titleString getStringLineSpacing:5 font:[UIFont systemFontOfSize:18 weight:UIFontWeightMedium] width:ScreenWidth*0.6];
    titleHeight += 30;
    CGFloat totalHieght = titleHeight +51;
    
    UIView *contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth*0.65, totalHieght)];
    contentView.backgroundColor = [UIColor darkGrayColor];
    contentView.layer.cornerRadius = 10;
    contentView.layer.masksToBounds = YES;
    contentView.center = self.center;
    [self addSubview:contentView];
    
    CGFloat contentViewWidth = contentView.bounds.size.width;
    CGFloat contentViewHeight = contentView.bounds.size.height;
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, contentViewWidth-30, titleHeight)];
    titleLabel.text = titleString;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightMedium];
    titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    titleLabel.numberOfLines = 0;
    [contentView addSubview:titleLabel];
    
    UILabel *topLineLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(titleLabel.frame), contentViewWidth, 1)];
    topLineLabel.backgroundColor = UIColorFromHex(0xdedede);
    [contentView addSubview:topLineLabel];
    
    UILabel *centerLine = [[UILabel alloc]initWithFrame:CGRectMake(contentViewWidth/2-0.4, CGRectGetMaxY(topLineLabel.frame), 0.8, contentViewHeight-CGRectGetMaxY(topLineLabel.frame))];
    centerLine.backgroundColor = UIColorFromHex(0xdedede);
    [contentView addSubview:centerLine];
    
    UIButton *confirmButton = [[UIButton alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(topLineLabel.frame), (contentViewWidth-0.8)/2, 50)];
    [confirmButton setTitle:NSLocalizedString(@"yes", @"是") forState:UIControlStateNormal];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    confirmButton.backgroundColor = [UIColor clearColor];
    [contentView addSubview:confirmButton];
    [confirmButton handleEven:UIControlEventTouchUpInside block:^(UIButton * _Nonnull sender) {
        NSLog(@"点击了是");
        if (self.delegate && [self.delegate respondsToSelector:@selector(AutoAlertViewComfirmButtonClicked)]) {
            [self.delegate AutoAlertViewComfirmButtonClicked];
        }
        [self removeFromSuperview];
    }];
    
    UIButton *cancleButton = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(centerLine.frame), CGRectGetMaxY(topLineLabel.frame), (contentViewWidth-0.8)/2, 50)];
    [cancleButton setTitle:NSLocalizedString(@"no", @"否") forState:UIControlStateNormal];
    [cancleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    cancleButton.backgroundColor = [UIColor clearColor];
    [contentView addSubview:cancleButton];
    [cancleButton handleEven:UIControlEventTouchUpInside block:^(UIButton * _Nonnull sender) {
        NSLog(@"点击了否");
        [self removeFromSuperview];
    }];
    
}

@end
